import 'package:flutter/material.dart';
import 'package:edu_assistant/widgets/common_image_view.dart';

class ImageViewPlayerPage extends StatefulWidget {
  final String url;
  final String title;
  ImageViewPlayerPage({super.key, required this.url, required this.title});

  @override
  _ImageViewPlayerPage createState() => _ImageViewPlayerPage();
}

class _ImageViewPlayerPage extends State<ImageViewPlayerPage> with SingleTickerProviderStateMixin {
  final TransformationController _transformationController =
  TransformationController();
  late AnimationController _animationController;
  Animation<Matrix4>? _animation;
  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 300),
    )..addListener(() {
      _transformationController.value = _animation!.value;
    });
  }
  void _handleDoubleTap() {
    // 创建复原动画
    _animation = Matrix4Tween(
      begin: _transformationController.value,
      end: Matrix4.identity(), // 单位矩阵 = 复原状态
    ).animate(
      CurveTween(curve: Curves.easeInOut).animate(_animationController),
    );

    _animationController.forward(from: 0);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: GestureDetector(
        onDoubleTap: _handleDoubleTap,
        child: InteractiveViewer(
          transformationController: _transformationController,
          panEnabled: true, // 启用平移
          boundaryMargin: const EdgeInsets.all(double.infinity),
          minScale: 0.1, // 最小缩放比例
          maxScale: 5.0, // 最大缩放比例
          child: Center(
            child: CommonImageView(
              url: widget.url,
              width: MediaQuery.of(context).size.width,
              height: MediaQuery.of(context).size.height,
            ),
          ),
        ),
      ),
    );
  }
}
